Java JavaScript Python C# C C++ Go Kotlin PHP Swift R Ruby TypeScript Scala SQL Perl rust VisualBasic Matlab Julia

PriorityQueue → PriorityQueue constructors

PriorityQueue

PriorityQueue constructors

Constructors in PriorityQueue

In PriorityQueue, constructors are special methods used to initialize a new instance of the queue. They define how the queue will be created and configured, including its initial capacity and ordering behavior. ⯌ PriorityQueue() ⯌ PriorityQueue(Collection<E> c) ⯌ PriorityQueue(int initialCapacity) ⯌ PriorityQueue(int initialCapacity, Comparator<E> comparator) ⯌ PriorityQueue(PriorityQueue<E> c) ⯌ PriorityQueue(SortedSet<E> c)

1. PriorityQueue():

This creates a PriorityQueue with the default initial capacity (usually 11) that orders elements according to their natural ordering (assuming they implement Comparable).
PriorityQueue Comparator Example import java.util.*; public class Main { public static void main(String[] args) { PriorityQueue<String> names = new PriorityQueue<>(); names.add("Sathish"); names.add("Mohan"); names.add("Deva"); // Order will be based on natural string ordering (alphabetical) System.out.println(names.poll()); } }

Output

Deva

2. PriorityQueue(Collection c):

This creates a PriorityQueue containing the elements from the specified collection. The ordering remains the same as in the original collection (if it's a sorted collection).
Priorityqueue with collection example in java import java.util.*; public class Main { public static void main(String[] args) { List<Integer> numbers = Arrays.asList(5, 3, 1, 4, 2); PriorityQueue<Integer> pq = new PriorityQueue<>(numbers); System.out.println(pq.poll()); } }

Output

1

3. PriorityQueue(int initialCapacity):

This constructor creates a PriorityQueue with the specified initial capacity that orders elements according to their natural ordering. This helps avoid frequent resizing when you know the approximate number of elements beforehand.
PriorityQueue initial cap constructor example in java import java.util.*; public class Main { public static void main(String[] args) { PriorityQueue<Integer> scores = new PriorityQueue<>(20); scores.add(85); scores.add(92); scores.add(78); // Order will be based on natural integer ordering (lowest first) System.out.println(scores.poll()); } }

Output

78

4. PriorityQueue(int initialCapacity, Comparator comparator):

This creates a PriorityQueue with the specified initial capacity and orders elements according to the provided comparator. This allows you to define custom logic for priority comparison even if elements don't implement Comparable. Example (custom object with comparator):
PriorityQueue comparator examples import java.util.*; class Person implements Comparable<Person> { String name; int age; public Person(String name, int age) { this.name = name; this.age = age; } @Override public int compareTo(Person other) { return this.age - other.age; // Compare by age directly (no getter) } } public class Main { public static void main(String[] args) { Comparator<Person> ageComparator = null; // Not needed with Comparable PriorityQueue<Person> people = new PriorityQueue<>(10); people.add(new Person("Sathish", 30)); people.add(new Person("Mohan", 25)); people.add(new Person("Deva", 35)); // Order will be based on age (youngest first) System.out.println(people.poll().age); // Access member directly (no getter) } }

Output

25

5. PriorityQueue(PriorityQueue c):

This Creates a new PriorityQueue containing all the elements from another PriorityQueue. The ordering remains the same as in the original queue. Example:
PriorityQueue(PriorityQueue c) example in java - one to another import java.util.*; public class Main { public static void main(String[] args) { PriorityQueue<String> tasks = new PriorityQueue<>(); tasks.add("Tutorialix.com"); tasks.add("Java tutorial"); tasks.add("Collection frameworks"); PriorityQueue<String> secondTasks = new PriorityQueue<>(tasks); System.out.println(tasks); // Order will be the same as the original queue System.out.println(secondTasks.poll()); } }

Output

[Collection frameworks, Tutorialix.com, Java tutorial] Collection frameworks

6. PriorityQueue(SortedSet c):

(Java 11+) Creates a PriorityQueue containing all the elements from a SortedSet. The ordering is preserved based on the natural ordering of the elements in the SortedSet. Example (assuming you have a SortedSet implementation):
PriorityQueue(SortedSet c) comparator example in java import java.util.*; public class Main { public static void main(String[] args) { SortedSet<Integer> sortedNumbers = new TreeSet<>(); sortedNumbers.add(2); sortedNumbers.add(5); sortedNumbers.add(4); sortedNumbers.add(7); sortedNumbers.add(9); PriorityQueue<Integer> pq = new PriorityQueue<>(sortedNumbers); System.out.println(sortedNumbers); // Order will be based on the natural ordering of the SortedSet (ascending) System.out.println(pq.poll()); } }

Output

[2, 4, 5, 7, 9] 2


Tutorials